<?php
/**
 * 静态化插件
 *design by Uncle Liu
 */
!defined('EMLOG_ROOT') && exit('access deined!');
function plugin_setting_view()
{
	require_once('../content/plugins/html/time.php');
?>
<script>
$("#html").addClass('sidebarsubmenu1');
</script>
<div class=containertitle><b>生成HTML</b>
<?php if(isset($_GET['setting'])):?><span class="actived">更新完成</span><?php endif;?>
</div>
<div class=line></div>
<div>
	<div>
   	<form id="form2" name="form2" method="post" action="plugin.php?plugin=html">
   	 自动更新时间: <input type="text" name="time" value="<?php echo HTML_TIME;?>" size="6"/>  <input type="submit" value="修改" name="changetime"> 单位:秒 1小时=3600秒 1天=86400秒 1周=604800秒<br/><br/>
   	<input name="delhtml" type="submit" value="删除所有HTML"   />  <input name="html" type="submit" value="更新所有HTML" /><input name="list" type="submit" value=" 更新列表 " /><br/>
<?php
   	if(!@is_writeable('../'))
	{
		echo "<br/><font color='red'>经检测博客根目录不可写，请修改权限为可写。</font>";
	}
?>
   	<br/>更新所有HTML会比较缓慢,与当前网速和主机有关,请耐心等待一会<br/>
	</form>
	</div>
	<br/>
<?php 
	if(isset($_POST['changetime']))//设置前台更新时间
	{
		$html_config = "<?php define('HTML_TIME',$_POST[time]);?>";
		$html_fso = @fopen('../content/plugins/html/time.php','w');//写入替换后的配置文件
		@fwrite($html_fso,$html_config);
		@fclose($html_fso);
		echo "<script>location.href='plugin.php?plugin=html'</script>";
	}
?>
<?php 
	if(isset($_POST['delhtml']))
	{
		$html_start_time = time();
		global $DB;

		//删除日志HTML
		$html_list = $DB->query("select * from ".DB_PREFIX."blog ");
		while($html_row = $DB->fetch_array($html_list))
		{
			if(@unlink("../post-".$html_row['gid'].".html"))
			{
				echo "删除".$html_row['title']." 成功.<br/>";
			}else{
				echo "删除".$html_row['title']." 失败,请登陆FTP手动删除<br/>";	
			}
		}

		//删除分类HTML
		$html_sortlist = $DB->query("select * from ".DB_PREFIX."sort");
		while($html_row = $DB->fetch_array($html_sortlist))
		{
			if(@unlink("../sort-".$html_row['sid'].".html"))
			{
				echo "删除分类 ".$html_row['sortname']."成功<br/>";
			}else{
				echo "删除分类 ".$html_row['sortname']."失败,请登陆FTP手动删除<br/>";
			}
		}

		//删除分页
		$html_num = $DB->num_rows($html_list);
		$html_index_lognum = $DB->fetch_row($DB->query("select * from ".DB_PREFIX."options  where `option_name`='index_lognum'"));
		$html_page = ceil($html_num / $html_index_lognum[2]);
		for($html_i = 1; $html_i <= $html_page; $html_i++)
		{
			if(@unlink("../page-".$html_i.".html"))
			{
				echo "删除page ".$html_i."成功<br/>";
			}else{
				echo "删除page ".$html_i."失败,请登陆FTP手动删除<br/>";
			}
		}

		//删除作者列表
		$html_author = $DB->query("select * from ".DB_PREFIX."user");
		while($html_authorrow = $DB->fetch_array($html_author))
		{
			if(@unlink("../author-".$html_authorrow['uid'].".html"))
			{
				echo "删除author-".$html_authorrow['uid']."成功<br/>";
			}else{
				echo "删除author-".$html_authorrow['uid'].".html失败,请登陆FTP手动删除<br/>";
			}
		}

		//删除标签HTML
		$html_tag = $DB->query("select * from ".DB_PREFIX."tag");
		while($html_tagrow = $DB->fetch_array($html_tag))
		{
			$html_utftag = $html_tagrow['tagname'];
			$html_utftag = str_replace(" ","+",$html_utftag);
			$html_gbktag = iconv("UTF-8", "GBK", $html_tagrow['tagname']);
			$html_gbktag = str_replace(" ","+",$html_gbktag);
			if(@unlink("../tag-".$html_gbktag.".html") || @unlink("../tag-".$html_utftag.".html"))
			{
				echo "删除tag-".$html_tagrow['tagname']."成功<br/>";
			}else{
				echo "删除tag-".$html_tagrow['tagname'].".html失败,请登陆FTP手动删除<br/>";
			}
			$html_tagurl=urlencode($html_tagrow['tagname']);
			@unlink("../tag-".$html_utftag.".html");//去残留
			@unlink("../tag-".$html_gbktag.".html");
			@unlink("../tag-".$html_tagurl.".html");//删除因编码不同产生的多余tag
		}

		//删除存档HTML
		$html_recordsfile = '../content/cache/records';
		$html_records = unserialize(implode('',file($html_recordsfile)));
		foreach ($html_records as $html_id => $html_row)
		{
			$html_recdate = str_replace("?record=","",$html_row['url']);
			if(@unlink("../record-".$html_recdate.".html"))
			{
				echo "删除record-".$html_recdate."成功<br/>";
			}else{
				echo "删除record-".$html_recdate.".html失败,请登陆FTP手动删除<br/>";
			}
		}
		
		$html_end_time = time();
		$html_used_time = $html_end_time - $html_start_time;
		echo "<script>alert('删除完毕,耗时".$html_used_time."秒')</script>";
	}
	if(isset($_POST['html']))
	{
		$html_start_time = time();
		@set_time_limit(0);

		//生成日志HTML
		global $DB;
		$html_list = $DB->query("select * from ".DB_PREFIX."blog order by gid DESC");
		while($html_row = $DB->fetch_array($html_list))
		{
			if($html_row['hide'] == 'n')
			{
				$html_blogfile = file_get_contents(BLOG_URL."?post=".$html_row['gid']);
				$html_blogfile = str_replace("<form action=\"\" method=\"post\">","<form action=\"./?post=".$html_row['gid']."\" method=\"post\">",$html_blogfile);
				$html_writehtml = @fopen("../post-".$html_row['gid'].".html","w");
				if(@fwrite($html_writehtml,$html_blogfile))
				{
					echo "生成日志 <a target='_blank' href='../post-".$html_row['gid'].".html'>".$html_row['title']."</a> 成功<br/>";
				}else {
					echo "生成日志 ".$html_row['title']."失败,请检查目录权限<br/>";
				}
			@fclose($html_writehtml);}
		}

		//生成分类HTML
		$html_sortlist = $DB->query("select * from ".DB_PREFIX."sort");
		while($html_row = $DB->fetch_array($html_sortlist))
		{
			$html_blogfile = file_get_contents(BLOG_URL."?sort=".$html_row['sid']);
			$html_writehtml = @fopen("../sort-".$html_row['sid'].".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				echo "生成分类 <a target='_blank' href='../sort-".$html_row['sid'].".html'>".$html_row['sortname']."</a> 成功<br/>";
			}else{
				echo "生成分类 ".$html_row['sortname']."失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}

		//生成分页HTML
		$html_num = $DB->num_rows($html_list);
		$html_index_lognum = $DB->fetch_row($DB->query("select * from ".DB_PREFIX."options  where `option_name`='index_lognum'"));
		$html_page = ceil($html_num / $html_index_lognum[2]);
		for($html_i = 1; $html_i <= $html_page; $html_i++)
		{
			$html_blogfile = file_get_contents(BLOG_URL."?page=".$html_i);
			$html_writehtml = @fopen("../page-".$html_i.".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				echo "生成列表 <a target='_blank' href='../page-".$html_i.".html'>第 ".$html_i." 页</a> 成功<br/>";
			}else{
				echo "生成列表第 ".$html_i." 页失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}

		//生成作者列表
		$html_author = $DB->query("select * from ".DB_PREFIX."user");
		while($html_authorrow = $DB->fetch_array($html_author))
		{
			$html_blogfile = file_get_contents(BLOG_URL."?author=".$html_authorrow['uid']);
			$html_writehtml = @fopen("../author-".$html_authorrow['uid'].".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				echo "生成列表 <a target='_blank' href='../author-".$html_authorrow['uid'].".html'>author- ".$html_authorrow['uid'].".html</a> 成功<br/>";
			}else{
				echo "生成列表author-".$html_authorrow['uid'].".html 失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}

		//生成标签HTML
		$html_tag = $DB->query("select * from ".DB_PREFIX."tag");
		while($html_tagrow = $DB->fetch_array($html_tag))
		{
			$html_utftag = $html_tagrow['tagname'];
			$html_utftag = str_replace(" ","+",$html_utftag);
			$html_gbktag = iconv("UTF-8", "GBK", $html_tagrow['tagname']);
			$html_gbktag = str_replace(" ","+",$html_gbktag);
			$html_tagurl = urlencode($html_tagrow['tagname']);
			$html_blogfile = file_get_contents(BLOG_URL."?tag=".$html_tagurl);
			$html_writehtml = @fopen("../tag-".$html_gbktag.".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				if(!@file_get_contents(BLOG_URL."tag-".$html_tagurl.".html"))//查看已生成的GBK格式TAG能否访问,不能则继续生成UTF格式
				{
					$html_writetag = @fopen("../tag-".$html_utftag.".html","w");
					if(@fwrite($html_writetag,$html_blogfile))
					{
						echo "生成标签 <a target='_blank' href='../tag-".$html_tagurl.".html'>tag- ".$html_tagrow['tagname'].".html</a> 成功<br/>";
						@fclose($html_writetag);
					}else{
						echo "生成标签 <a target='_blank' href='../tag-".$html_tagurl.".html'>tag- ".$html_tagrow['tagname'].".html</a>失败<br/>";	
					}
				}else{
					echo "生成标签 <a target='_blank' href='../tag-".$html_tagurl.".html'>tag- ".$html_tagrow['tagname'].".html</a> 成功<br/>";
				}
			}else{
				echo "生成标签tag-".$html_tagrow['tagname'].".html 失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}

		//生成存档HTML
		$html_recordsfile = '../content/cache/records';
		$html_records = unserialize(implode('',file($html_recordsfile)));
		foreach ($html_records as $html_id => $html_row)
		{
			$html_recdate = str_replace("?record=","",$html_row['url']);
			$html_blogfile = file_get_contents(BLOG_URL.$html_row['url']);
			$html_writehtml = @fopen("../record-".$html_recdate.".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				echo "生成存档 <a target='_blank' href='../record-".$html_recdate.".html'>recdate- ".$html_recdate.".html</a> 成功<br/>";
			}else{
				echo "生成存档recdate-".$html_recdate.".html 失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}

		$html_end_time = time();
		$html_used_time = $html_end_time - $html_start_time;
		echo "<script>alert('更新完毕,耗时".$html_used_time."秒')</script>";
	}
?>
<?php 
	if(isset($_POST['list']))//更新列表分页
	{
		$html_start_time = time(); 
		global $DB;
		$html_list = $DB->query("select * from ".DB_PREFIX."blog order by gid DESC");
		$html_num = $DB->num_rows($html_list);
		$html_index_lognum = $DB->fetch_row($DB->query("select * from ".DB_PREFIX."options  where `option_name`='index_lognum'"));
		$html_page = ceil($html_num / $html_index_lognum[2]);
		for($html_i = 1; $html_i <= $html_page; $html_i++)
		{
			$html_blogfile = file_get_contents(BLOG_URL."?page=".$html_i);
			$html_writehtml = @fopen("../page-".$html_i.".html","w");
			if(@fwrite($html_writehtml,$html_blogfile))
			{
				echo "生成列表 <a target='_blank' href='../page-".$html_i.".html'>第 ".$html_i." 页</a> 成功<br/>";
			}else{
				echo "生成列表第 ".$html_i." 页失败,请检查目录权限<br/>";
			}
			@fclose($html_writehtml);
		}
		$html_end_time = time();
		$html_used_time = $html_end_time - $html_start_time;
		echo "<script>alert('列表更新完毕,耗时".$html_used_time."秒')</script>";
	}
?>
</div>
<?php
}
?>